//
//  HTMLTableViewCell.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "HTMLTableViewCell.h"
#import "LHMGetHeightTool.h"
#import "SmileModel.h"
#import "SmileWebViewController.h"

@interface HTMLTableViewCell ()

@property (nonatomic, retain) UILabel *titleLable;
@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UIButton *imageButton;

@end

@implementation HTMLTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.titleLable = [[UILabel alloc] init];
        [self.contentView addSubview: _titleLable];
        
        self.myImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_myImageView];
        
        self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_imageButton];
        
    }
    return self;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    NSString *title = [self.smileModel.html objectForKey:@"title"];
    
    CGFloat titleHeight = [LHMGetHeightTool heightWith:title];
    self.titleLable.frame = CGRectMake(10, 35, self.contentView.bounds.size.width - 20, titleHeight);
    
    self.titleLable.numberOfLines = 0;
    self.titleLable.backgroundColor = [UIColor whiteColor];
    
    self.imageButton.frame = CGRectMake(10, titleHeight + 35, self.contentView.bounds.size.width - 20, 200);
    [self.imageButton addTarget:self action:@selector(inWebView:) forControlEvents:UIControlEventTouchUpInside];
    self.imageButton.backgroundColor = [UIColor whiteColor];
    
}

-(void)setSmileModel:(SmileModel *)smileModel {
    
    NSString *title = [smileModel.html objectForKey:@"title"];
    
    self.titleLable.text = title;
    
    NSArray *thumArray = [smileModel.html objectForKey:@"thumbnail"];
    
    NSString *thum = [thumArray objectAtIndex:0];
    
    NSURL *url = [NSURL URLWithString:thum];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:data];
    
    [self.imageButton setImage:image forState:UIControlStateNormal];
    
}

-(void)inWebView:(UIButton *)button {
    
    SmileWebViewController *webVC = [[SmileWebViewController alloc] init];
    
    webVC.smileModel = self.smileModel;
    
    [[self naviController] pushViewController:webVC animated:YES];
    
}

- (UINavigationController*)naviController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController*)nextResponder;
        }
    }
    return nil;
}



@end
